package com.fitness.front.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.MemberWalletConvertor;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.modal.wallet.entity.MemberWallet;
import com.fitness.front.modal.wallet.params.RechargeVOQueryParams;
import com.fitness.front.modal.wallet.vo.MemberWalletVO;
import com.fitness.front.modal.wallet.vo.RechargeVO;
import com.fitness.front.service.IMemberWalletService;
import com.fitness.front.service.IRechargeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/wallet")
public class PaymentController {

    private final IMemberWalletService memberWalletService;
    private final IRechargeService rechargeService;

    private final MemberWalletConvertor memberWalletConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/rechargePage")
    public ServerResponseEntity<IPage<RechargeVO>> page(RechargeVOQueryParams queryParams) {
        Page<RechargeVO> result = rechargeService.getPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 获取表单
     */
    @GetMapping("/getMemberWallet")
    public ServerResponseEntity<MemberWalletVO> getMemberWallet(Long memberId) {
        QueryWrapper<MemberWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        MemberWallet result = memberWalletService.getOne(queryWrapper);
        MemberWalletVO memberWalletVO = memberWalletConvertor.entity2Vo(result);
        return ServerResponseEntity.success(memberWalletVO);
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteBilling")
    public ServerResponseEntity<Boolean> deleteBilling(Long id) {
        return ServerResponseEntity.success(rechargeService.removeById(id));
    }

    /**
     * 支付
     */
    @RequestMapping("/payment")
    public ServerResponseEntity<Boolean> payment(@RequestParam("memberId") Long memberId,
                                                 @RequestParam("memberName") String memberName,
                                                 @RequestParam("amount") Long amount,
                                                 @RequestParam("comment")String remark) {
        if(StrUtil.isBlank(remark)){
            ServerResponseEntity.fail("P00001","未填写支付账单用途");
        }
        boolean result = memberWalletService.payment(memberId,memberName,amount,remark);
        return ServerResponseEntity.success(result);
    }

    /**
     * 退款
     */
    @RequestMapping("/refund")
    public ServerResponseEntity<Boolean> refund(@RequestParam("memberId") Long memberId,
                                                 @RequestParam("memberName") String memberName,
                                                 @RequestParam("amount") Long amount,
                                                @RequestParam("remark")String remark) {
        if(StrUtil.isBlank(remark)){
            ServerResponseEntity.fail("P00001","未填写支付账单用途");
        }
        boolean result = memberWalletService.refund(memberId,memberName,amount,remark);
        return ServerResponseEntity.success(result);
    }

    /**
     * 充值
     */
    @RequestMapping("/recharge")
    public ServerResponseEntity<Boolean> recharge(@RequestParam("memberId") Long memberId,
                                                @RequestParam("memberName") String memberName,
                                                @RequestParam("amount") Long amount,
                                                  @RequestParam("remark")String remark) {
        if(StrUtil.isBlank(remark)){
            ServerResponseEntity.fail("P00001","未填写支付账单用途");
        }
        boolean result = memberWalletService.recharge(memberId,memberName,amount,remark);
        return ServerResponseEntity.success(result);
    }
}
